// 商品属性模型
const { DataTypes } = require('sequelize');
const { sequelize } = require('../utils/sequelize');
const config = require('../config/database');

const GoodsAttributes = sequelize.define(
  'BidGoodsAttributes', // 模型名称
  {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true,
      allowNull: false,
      comment: '属性ID'
    },
    goods_id: {
      type: DataTypes.STRING(50),
      allowNull: false,
      comment: '商品ID',
      // 外键约束，引用商品表的ID
      references: {
        model: config.mysql.prefix + 'goods',
        key: 'id'
      },
      onDelete: 'CASCADE'
    },
    attr_key: {
      type: DataTypes.STRING(50),
      allowNull: false,
      comment: '属性名称'
    },
    attr_value: {
      type: DataTypes.STRING(255),
      allowNull: false,
      comment: '属性值'
    }
  },
  {
    tableName: config.mysql.prefix + 'goods_attributes',
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: false,
    indexes: [
      { fields: ['goods_id'], name: 'idx_goods_id' }
    ]
  }
);

module.exports = GoodsAttributes;